#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <UIKit/UIApplication.h>
#import <LockInfo/Plugin.h>

#import "framework/FWCommon.h"
#import "framework/FWPlugin.h"
#import "framework/FWDotView.h"
#import "framework/FWDetailListEntry.h"
#import "framework/FWDetailListSection.h"

#import "PICommon.h"
#import "PIAlarm.h"
#import "PIPreview.h"
#import "PIAttendee.h"
#import "PIIntegration.h"

#import "PIObject.h"


@implementation PIPreview


// ******************************************************************************************
#pragma mark -
#pragma mark Alarms section

- (NSArray*)alarmsListForObject:(PIObject*)obj dueDate:(NSDate*)dueDate bundle:(NSBundle*)bundle application:(FWApplication*)app
{
    if (obj.alarms) {
        NSMutableArray* alarmsSect = [NSMutableArray arrayWithCapacity:obj.alarms.count];

        NSMutableArray* alarms = [NSMutableArray arrayWithArray:obj.alarms];
        [alarms sortUsingFunction:compareAlarmTimes context:nil];
        
        NSInteger count = 0;
        for (PIAlarm* alarm in alarms) {
            NSMutableString* description = [NSMutableString stringWithCapacity:60];
            if (alarm.isRelative) {
                NSTimeInterval secs = -alarm.relativeDate;
                if (secs >= SECONDS_PER_DAY) {
                    int days = secs / SECONDS_PER_DAY;
                    secs -= days * SECONDS_PER_DAY;
                    if (days > 0)  
                        [description appendFormat:localize(bundle, days == 1 ? @"Detail/Alarm/Day" : @"Detail/Alarm/Days"), days];
                }
                if (secs >= SECONDS_IN_HOUR) {
                    int hours = secs / SECONDS_IN_HOUR;
                    secs -= hours * SECONDS_IN_HOUR;
                    if (description.length > 0)
                        [description appendString:@" "];
                    if (hours > 0)
                        [description appendFormat:localize(bundle, hours == 1 ? @"Detail/Alarm/Hour" : @"Detail/Alarm/Hours"), hours];
                }
                if (secs >= SECONDS_IN_MINUTE) {
                    int mins = secs / SECONDS_IN_MINUTE;
                    secs -= mins * SECONDS_IN_MINUTE;
                    if (description.length > 0)
                        [description appendString:@" "];
                    if (mins > 0)
                        [description appendFormat:localize(bundle, mins == 1 ? @"Detail/Alarm/Minute" : @"Detail/Alarm/Minutes"), mins];
                }
                if ((secs > 0) && (secs < SECONDS_IN_MINUTE)) {
                    if (description.length > 0)
                        [description appendString:@" "];
                    [description appendFormat:localize(bundle, secs == 1 ? @"Detail/Alarm/Second" : @"Detail/Alarm/Seconds"), (int)secs];
                }
                description = [NSMutableString stringWithFormat:localize(bundle, @"Detail/Alarm/Relative"), description];
            }
            else {
                // Absolute alarm
                BOOL showDate = !FWIsLocalDateSameDay(dueDate, alarm.date);
                [description appendString:FWLocalDateStringForUTCDate(alarm.date, showDate ? FWDateFormatterNormalFormat : FWDateFormatterTimeOnlyFormat, YES, bundle)];
            }
            
            if (app.fwPlugin.isDebugMode)
                [description appendFormat:@" (%@)", alarm.date];

            NSString* title = localize(bundle, alarms.count == 1 ? @"Detail/Alarm" : @"Detail/Alarms");
            if (alarms.count > 1)
                title = [NSString stringWithFormat:title, ++count];
            
            [alarmsSect addObject:[FWDetailListEntry entryWithTitle:title
                                                              value:description]];
        }
        
        return alarmsSect;
    }

    // No alarms
    return [NSArray array];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Repeat section

- (NSArray*)repeatSectionForObject:(PIObject*)obj bundle:(NSBundle*)bundle application:(FWApplication*)app
{
    if (obj.recurrance) {
        NSMutableArray* repeatSect = [NSMutableArray arrayWithCapacity:2];
        
        FWWeekdayName weekend = (FWWeekdayName)[app.fwPlugin.preferences
                                                intValue:kPIOptionsWeekendStart
                                                withDefault:kPIOptionsWeekendStartDefault];
        
        if (NSString* interval = [obj.recurrance intervalDescriptionWithWeekendStart:weekend])
            [repeatSect addObject:
             [FWDetailListEntry entryWithLocalisedTitle:@"Detail/Repeat/Interval"
                                                  value:interval
                                                 bundle:bundle]];
        if (NSString* until = obj.recurrance.untilDescription)
            [repeatSect addObject:
             [FWDetailListEntry entryWithLocalisedTitle:@"Detail/Repeat/End"
                                                  value:until
                                                 bundle:bundle]];
        
        return repeatSect;
    }
    
    // No info
    return [NSArray array];
}


// ******************************************************************************************
#pragma mark -
#pragma mark Common look and feel functions

- (FWDetailListEntry*)detailColourEntryForTitle:(NSString*)title value:(NSString*)value icon:(NSString*)icon calendarIcon:(NSString*)calendarIcon calendarColor:(UIColor*)calendarColor
{
    FWDetailListEntry* entry;
    if (value)
        entry = [FWDetailListEntry entryWithTitle:title
                                            value:[PIIntegration stringForTitle:value
                                                                       withIcon:icon
                                                                withProjectIcon:calendarIcon]];
    else
        entry = [FWDetailListEntry entryWithTitle:[PIIntegration stringForTitle:title
                                                                       withIcon:icon
                                                                withProjectIcon:calendarIcon]];
    
    if (calendarColor && ![calendarColor isEqual:[UIColor clearColor]]) {
        FWDotView* calIcon = [[[FWDotView alloc] initWithFrame:CGRectMake(0, 0,  18, 18) shadow:YES] autorelease];
        calIcon.color = calendarColor;
        entry.accessoryView = calIcon;
    }
    
    return entry;
}

@end
